package cn.sjtu.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "index";
    }

    @RequestMapping("/world")
    public String world() {
        return "index";
    }

    @RequestMapping("/r/p1")
    @ResponseBody
    public String getP1() {
        UserDetails userDetails =
                (UserDetails) SecurityContextHolder
                        .getContext()
                        .getAuthentication()
                        .getPrincipal();
        return userDetails.getUsername() + "欢迎访问p1";
    }

    @RequestMapping("/r/p2")
    @ResponseBody
    public String getP2() {
        UserDetails userDetails =
                (UserDetails) SecurityContextHolder
                        .getContext()
                        .getAuthentication()
                        .getPrincipal();
        return userDetails.getUsername() + "欢迎访问p2";
    }

}
